package restaraunt.rating.project;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class Login extends Activity {

	// Declare Variables
	public static String fileName = "loginData";
	boolean saveData, loginSuccess;
	
	// Declare Controls
	Button bLog;
	EditText tUsr, tPsw;
	CheckBox cRem;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		// Init. Variables
			// (no variables yet)
		
		// Init. Controls
		bLog = (Button) findViewById(R.id.btLogin);
		tUsr = (EditText) findViewById(R.id.etUsername);
		tPsw = (EditText) findViewById(R.id.etPassword);
		cRem = (CheckBox) findViewById(R.id.cbRemember);
		
		// Load and retrieve login data, if present.
		procFile(fileName, 'r');
		
		// Init. Listeners
		bLog.setOnClickListener(new View.OnClickListener() {
		
			public void onClick(View v) {
				// Part 1: Submit login info to database
				
				/* ENTER LOGIN CODE HERE */
				
				// loginSuccess should be assigned depending on the success or failure
				// of the login attempt. I am assigning it to true just to make sure
				// post-login features are working correctly.
				loginSuccess = true;
				
				// Change file data according to remember settings
				if (loginSuccess && cRem.isChecked())
				{
					// If chosen to remember, writes data to file
					procFile(fileName, 'w');
				}
				else
				{
					// If not chosen to remember, deletes login data from file
					procFile(fileName, 'd');
				}
				
				// Part 3: Destroy activity, return to main
				Intent openMenu = new Intent("restaraunt.rating.project.MENU");
				startActivity(openMenu);
				finish();
			}
		});
	}
	
	public void procFile(String f, char rwMode)
	{
		/* 'r' Reads from source file
		 * 'w' Writes to source file
		 * 'd' Deletes keys from source file
		 */
		
		final SharedPreferences loginInfo = getSharedPreferences(fileName, 0);
		SharedPreferences.Editor editor = loginInfo.edit();
		
		if (rwMode == 'r')
		{
			String usernameData = loginInfo.getString("lgUser", "");
			String passwordData = loginInfo.getString("lgPass", "");
			
			if (usernameData != "" && passwordData != "")
			{
				// Check the box
				tUsr.setText(usernameData);
				tPsw.setText(passwordData);
				cRem.setChecked(true);
			}
			
			editor.commit();
		}
		else if (rwMode == 'w')
		{
			// Retrieves strings from editText objects
			String usernameData = tUsr.getText().toString();
			String passwordData = tPsw.getText().toString();
			
			// Commits data to file
			editor.putString("lgUser", usernameData);
			editor.putString("lgPass", passwordData);
			editor.commit();
		}
		else if (rwMode == 'd')
		{
			// Deletes username and password from file
			editor.remove("lgUser");
			editor.remove("lgPass");
			editor.commit();
		}
		
		// Will do nothing if rwMode is not correctly defined as 'r', 'w', 'd'.
	}
}
